Namespace Framework.DAL
    ''' <summary>
    ''' This class is responsible for facilitating the hydration of <see cref="IHydratable" /> objects in the DAL.
    ''' </summary>
    ''' <remarks>
    ''' This class is responsible for facilitating the hydration of <see cref="IHydratable" /> objects in the DAL.
    ''' It supports 
    ''' </remarks>
    Public MustInherit Class AbstractHydratable

        Private Function Instance(Of TObject)() As TObject
            Return CType(Activator.CreateInstance(GetType(TObject)), TObject)
        End Function

        Protected Function FillObject(Of TObject As IHydratable)(ByVal theDelegate As Hydrate, ByVal Reader As IDataReader) As TObject
            Dim Info As TObject = Nothing
            Using Reader
                If Not Reader.IsClosed AndAlso Reader.Read Then
                    Info = Instance(Of TObject)()
                    Info.Fill(theDelegate, Reader)
                End If
            End Using
            Return Info
        End Function

        Protected Function FillList(Of TObject As IHydratable)(ByVal theDelegate As Hydrate, ByVal Reader As IDataReader) As IList(Of TObject)
            Dim LotsOfInfo As New List(Of TObject)()
            Using Reader
                If Not Reader.IsClosed Then
                    Dim Info As TObject = Nothing
                    While Reader.Read
                        Info = Instance(Of TObject)()
                        Info.Fill(theDelegate, Reader)
                        LotsOfInfo.Add(Info)
                    End While
                End If
            End Using
            Return LotsOfInfo
        End Function

        ''' <summary>
        ''' AssertInstance ensures the ObjectToHydrate is not null and is of the correct type.
        ''' </summary>
        ''' <typeparam name="TObject">The data type to check against</typeparam>
        ''' <param name="ObjectToHydrate">The object instance to check for null and type</param>
        ''' <remarks>
        ''' AssertInstance ensures the ObjectToHydrate is not null and is of the correct type.
        ''' These assertions are restricted to hydratable business objects.
        ''' </remarks>
        Protected Shared Sub AssertInstance(Of TObject As AbstractInfo)(ByRef ObjectToHydrate As AbstractInfo)
            If ObjectToHydrate Is Nothing Then
                Throw New ArgumentNullException("ObjectToHydrate is null", "ObjectToHydrate")
            End If
            If Not TypeOf ObjectToHydrate Is TObject Then
                Throw New ArgumentException("Expected AbstractInfo to be a " + GetType(TObject).Name + " object", "ObjectToHydrate")
            End If
        End Sub
    End Class

End Namespace
